package com.manager.kudian.system.web;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.manager.kudian.common.persistence.Page;
import com.manager.kudian.common.persistence.PropertyFilter;
import com.manager.kudian.common.utils.StringUtils;
import com.manager.kudian.common.web.BaseController;
import com.manager.kudian.system.dao.GroupBuyDao;
import com.manager.kudian.system.entity.*;
import com.manager.kudian.system.service.*;
import com.manager.kudian.system.vo.SellerVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.*;
import java.util.logging.Logger;

/**
 * 角色controller
 * @author ty
 * @date 2015年1月13日
 */
@Controller
@RequestMapping("business/seller")
public class SellerController extends BaseController{
	
	@Autowired
	private SellerService sellerService;

	@Autowired
	private UsersService usersService;

	@Autowired
	private GoodsTypeService goodsTypeService;

	@Autowired
	private MediaResService mediaResService;

	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "business/sellerList";
	}

	/**
	 * 获取字典json
	 */
	@RequestMapping(value="json",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> sellerList(HttpServletRequest request) {
//		Page<Seller> page = getPage(request);
//
//		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
//		page = sellerService.search(page, filters);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Page<Seller> page = getPage(request);
			sellerService.query(page);

			List<SellerVO> l = new ArrayList<SellerVO>();
			for (Seller s : page.getResult()) {
				SellerVO v = new SellerVO();
				BeanUtils.copyProperties(s, v);

				Users u = usersService.getOne(v.getUserId());
				// v.setUserName(u.getUserName());
				// v.setUserName(u.getMobilePhone());
				v.setUserName(u.getUserName());
				v.setRegPhone(u.getMobilePhone());

				GoodsType gt = goodsTypeService.getOne(v.getGtId());
				if (gt != null)
					v.setGtName(gt.getGtName());
				l.add(v);
			}

			map.put("rows", l);
			map.put("total", page.getTotalCount());

			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping(value = "auditSeller/{id}")
	@ResponseBody
	public String auditSeller(@PathVariable("id") Integer id, HttpSession session) {
		try {
			if (!sellerService.checkSellerName(id)) {
				return "未添加店铺名称，请先添加店铺名称";
			}
			ManagerUser u = (ManagerUser) session.getAttribute("user");
			this.sellerService.auditSeller(id, u);
		} catch (Exception e) {
			return "审核失败";
		}
		return "审核成功";
	}

	/**
	 * 审核不通过
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "auditNopassSeller/{id}")
	@ResponseBody
	public String auditNopassSeller(@PathVariable("id") Integer id, HttpSession session) {
		try {

			ManagerUser u = (ManagerUser) session.getAttribute("user");
			this.sellerService.auditNopassSeller(id, u);
		} catch (Exception e) {
			return "审核不通过更新失败";
		}
		return "审核不通过更新成功";
	}

	@RequestMapping(value = "sellerinfo/{id}", method = RequestMethod.GET)
	public String sellerinfo(@PathVariable("id") Integer id, Model model) {

		try {
			Seller s = sellerService.get(id);

			String busPics = mediaResService.getResUrls(s.getBusLicencePics());
			String corporateIcardPics = mediaResService.getResUrls(s.getCorporateIcardPics());
			String sellerPics = mediaResService.getResUrls(s.getSellerPics());

			if (StringUtils.isNotBlank(busPics)) {
				model.addAttribute("busPics", busPics.split(","));
			}
			if (StringUtils.isNotBlank(corporateIcardPics)) {
				model.addAttribute("corporateIcardPics", corporateIcardPics.split(","));
			}
			if (StringUtils.isNotBlank(sellerPics)) {
				model.addAttribute("sellerPics", sellerPics.split(","));
			}

			model.addAttribute("seller", s);
		} catch (Exception e) {
		}
		return "business/sellerinfo";
	}

	@RequestMapping(value = "updateInfo/{id}", method = RequestMethod.POST)
	@ResponseBody
	public String updateInfo(@PathVariable("id") Integer id, String name, String corporation, String bankAccount) {
		try {
			sellerService.updateinfo(id, name, corporation, bankAccount);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
}
